<template>
  <kk-container @handlerRefresh="handlerRefresh">
    <image class="topBg" src="@/static/images/home/home_top_bg.png"></image>
    <view class="navTitle" :style="{ paddingTop: navStatusHeight }"
      >库课云商家端</view
    >
    <TopTenantInfo />
    <TopSwiper />
    <view style="height: 24rpx" v-if="wxOpenIdRequest && !wxOpenid"></view>
    <FollowWxAccount v-if="wxOpenIdRequest && !wxOpenid" />
    <MyQAToDo :data="myQAToDoStaticsInfo" />
    <QaStatics :data="asnwerStaticsInfo" @selectStaticDate="selectStaticDate" />
  </kk-container>
</template>

<script setup lang="ts">
import { onShow } from "@dcloudio/uni-app";
import { ref, computed } from "vue";
import TopTenantInfo from "./components/topTenantInfo.vue";
import TopSwiper from "./components/topSwiper.vue";
import FollowWxAccount from "./components/followWxAccount.vue";
import MyQAToDo from "./components/myQAToDo.vue";
import QaStatics from "./components/qaStatics.vue";

import {
  saveQrCodeUrl,
  getQrCodeUrl,
  getQrCodeUrlIsExpire,
  getUserInfo,
} from "@/utils/storageUtil";

import * as KKAnswerApi from "@/api/answer";
import * as KKIndexApi from "@/api/index";

const wxOpenid = ref("");
const wxOpenIdRequest = ref(false);

const myQAToDoStaticsInfo = ref({});
// 答疑统计信息
const currentAnswerStaticsDate = ref("");
const asnwerStaticsInfo = ref({});

const navStatusHeight = computed(() => {
  let e = uni.getSystemInfoSync();
  return `${e.statusBarHeight}px`;
});

onShow(() => {
  uni.showTabBar();
  getWoaOpenId();
  getAppletTeacherBacklogStatistics();
  if (currentAnswerStaticsDate.value) {
    selectStaticDate(currentAnswerStaticsDate.value);
  }
});

// 获取公众号openid
const getWoaOpenId = async () => {
  let adminId = getUserInfo().adminUserId || "";
  if (!adminId) {
    wxOpenIdRequest.value = true;
    wxOpenid.value = "";
    return;
  }
  const data = await KKIndexApi.getWoaOpenId({ adminId }).catch((e) => {});
  if (data && data.data && data.data.openId) {
    wxOpenid.value = data.data.openId || "";
  } else {
    wxOpenid.value = "";
  }
  wxOpenIdRequest.value = true;
  if (!wxOpenid.value) {
    getMyQrCode();
  }
};

// 获取我的公众号二维码
const getMyQrCode = async () => {
  if (getQrCodeUrl() && !getQrCodeUrlIsExpire()) {
    return;
  }
  const data = await KKIndexApi.getWoaQrCodeProt().catch((e) => {});
  if (data && data.data && data.data.url) {
    saveQrCodeUrl(data.data.url || "");
  }
};

// 我得答疑代办统计
const getAppletTeacherBacklogStatistics = async () => {
  const data = (await KKAnswerApi.getAppletTeacherBacklogStatistics().catch(
    (e) => {}
  )) as any;
  if (data && data.data) {
    myQAToDoStaticsInfo.value = data.data;
  }
};

const selectStaticDate = async (date: string) => {
  currentAnswerStaticsDate.value = date;
  const data = (await KKAnswerApi.getAppletTeacherAnswerCountPort(date).catch(
    (e) => {}
  )) as any;
  if (data && data.data) {
    asnwerStaticsInfo.value = data.data;
  }
};
const handlerRefresh = () => {
  getAppletTeacherBacklogStatistics();
  if (currentAnswerStaticsDate.value) {
    selectStaticDate(currentAnswerStaticsDate.value);
  }
};
</script>

<style lang="scss" scoped>
.topBg {
  position: absolute;
  width: 100vw;
  height: 450rpx;
  z-index: -1;
}
.navTitle {
  text-align: center;
  font-weight: 500;
  font-size: 34rpx;
  color: #000000;
  line-height: 44px;
}
</style>
